from django.db import models


# Create your models here.
# 定义 部门 模型类
# name , varchar(30)
class Dep(models.Model):
    # 类属性
    name = models.CharField("部门名称", max_length=30)

    def __str__(self):
        # 必须返回一个字符串
        return "郭细雨"

        # print("123") - ---123
        # print(obj) - ----"郭细雨"

    class Meta:
        db_table = "dep_tb"  # 模型类映射到数据库中的表名
        verbose_name_plural = '部门表' # 模型类注册到后台管理的表名


# 定义 职务  模型类
# name , varchar(30)
class Duty(models.Model):
    name = models.CharField("职务名称", max_length=30)

    def __str__(self):
        return self.name    # 打印模型类的实例对象时，打印出的信息

    class Meta:
        db_table = "duty_tb"
        verbose_name_plural = "职务表"


# 定义员工模型类
# name varchar(30)
# sex int   1-男  2-女
# phone varchar(11)
# email varchar(60)
# department  fk
# duty  fk
class Staff(models.Model):
    SEX_CHOICE = (
        (1, "男"),
        (2, "女")
    )
    name = models.CharField("员工名称", max_length=30)
    sex = models.IntegerField(default=1, choices=SEX_CHOICE, verbose_name="性别")
    phone = models.CharField("手机号", max_length=11)
    email = models.CharField("邮箱", max_length=60)
    dep = models.ForeignKey(Dep, on_delete=models.CASCADE, verbose_name="所属部门")
    duty = models.ForeignKey(Duty, on_delete=models.CASCADE, verbose_name="职务")

    def __str__(self):
        return self.name

    class Meta:
        db_table = "staff_tb"
        verbose_name_plural = "员工表"


    # DRF   提供的序列化器 Serializer   &  ModelSerializer


# 模型类的自关联---单表存储
class Region(models.Model):
    name = models.CharField("名称", max_length=10)
    cid = models.ForeignKey("self", on_delete=models.CASCADE, verbose_name="上级类别",
                            null=True, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "region_tb"
        verbose_name_plural = "区域表"
